package com.ujiuye.request;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Map;
import java.util.Set;

@WebServlet("/rd4")
public class RequestDemo3 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String usrname = req.getParameter("username");
        System.out.println(usrname);
        String password = req.getParameter("password");
        System.out.println(password);
        String sex = req.getParameter("sex");
        System.out.println(sex);
        //根据参数名获取多个参数值
        String[] hobbies = req.getParameterValues("hobby");
        System.out.println(Arrays.toString(hobbies));
        //根据请求体中一个键获取请求体中一个的值
        String work = req.getParameter("work");
        System.out.println(work);
        String sd = req.getParameter("sd");
        System.out.println(sd);
        //获取所有的参数名
//        username
//                password
//        sex
//                hobby
//        work
//                sd
        Enumeration<String> parameterNames = req.getParameterNames();
        while (parameterNames.hasMoreElements()){
            String key1 = parameterNames.nextElement();
            System.out.println(key1);
        }
        // //将请求体中所有的数据接收到map对象中 其中map的key就是你提交键  map值就是你输入的内容
//        username[朱婷婷]
//        password[1234]
//        sex[man]
//        hobby[嫖, 吃, 喝]
//        work[四个女的]
//        sd[ 我是猛男]
        Map<String, String[]> parameterMap = req.getParameterMap();
        Set<Map.Entry<String, String[]>> entries = parameterMap.entrySet();
        for (Map.Entry<String, String[]> entry : entries) {
            String key = entry.getKey();
            String[] value = entry.getValue();
            System.out.println(key+Arrays.toString(value));
        }


    }
}
